//
//  MineInfoController.m
//  NoNameAmazing
//
//  Created by liuyang on 2017/7/6.
//  Copyright © 2017年 北京执行力科技有限公司. All rights reserved.
//

#import "MineInfoController.h"
#import "MineInfoCell.h"
#import "MineHandCell.h"
#import "ReSetController.h"
#import "ChangeImageController.h"
@interface MineInfoController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)UITableView * tableView;
@property(nonatomic,strong)NSArray * titleArray;
@end

@implementation MineInfoController
{
    NSURL *_URL;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self createTableView];
    [self requestHeadImage];
    self.title = @"个人信息";
    self.titleArray = [NSArray array];
    self.navigationItem.rightBarButtonItem = [UIBarButtonItem barButtonTitle:@"保存" image:nil frame:CGRectMake(0, 0, 30, 25) target:self sel:@selector(baoClick)];
    if ([[us objectForKey:kRoleid] isEqualToString:@"2"]) {
        self.titleArray = @[@"头像",@"职工号",@"姓名",@"性别",@"出生年月",@"部门",@"职位",@"身份证",@"联系电话"];
        
    }else{
       self.titleArray = @[@"头像",@"学号",@"姓名",@"性别",@"出生年月",@"院系",@"班级",@"身份证",@"银行卡号",@"联系电话"];
    }
    
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(SFZ:) name:@"SFZ" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(YHKH:) name:@"YHKH" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(LXDH:) name:@"LXDH" object:nil];
    
     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(head:) name:@"head" object:nil];

}

-(void)createTableView
{
    _tableView=[[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreen_Width, kScreen_Height-44) style:UITableViewStyleGrouped];
    _tableView.delegate=self;
    _tableView.dataSource=self;
    _tableView.showsVerticalScrollIndicator=NO;
    _tableView.showsHorizontalScrollIndicator=NO;
    _tableView.userInteractionEnabled= YES ;
    
    _tableView.contentInset = UIEdgeInsetsMake(0, 0, -5, 0);
    
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    [self.view addSubview:_tableView];
}

#pragma mark - 数据源
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return 1;
    } else if (section == 1){
        return 6;
    }else if (section == 2){
         if ([[us objectForKey:kRoleid] isEqualToString:@"2"]) {
             return 2;
         }else{
        return 3;
         }
    }
    
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section==0) {
         MineHandCell *cell = [[[NSBundle mainBundle] loadNibNamed:@"MineHandCell" owner:nil options:nil] lastObject];
        cell.selectionStyle = UITableViewCellSelectionStyleDefault;
        cell.userInteractionEnabled=YES;
        [cell.headImage sd_setImageWithURL:_URL];
        return cell;
    }else if (indexPath.section==1){
        MineInfoCell *cell = [[[NSBundle mainBundle] loadNibNamed:@"MineInfoCell" owner:nil options:nil] lastObject];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.userInteractionEnabled=NO;
         cell.btn.hidden = YES;
        cell.titleLab.text = self.titleArray[indexPath.row+1];
        if (indexPath.row==0) {
            cell.contentLab.text = self.minetInfoModel.userid;
        }else if (indexPath.row==1){
            cell.contentLab.text = self.minetInfoModel.xm;
        }else if (indexPath.row==2){
             cell.contentLab.text = self.minetInfoModel.xb;
        }else if (indexPath.row==3){
            cell.contentLab.text = self.minetInfoModel.birth;
        }else if (indexPath.row==4){
           cell.contentLab.text = self.minetInfoModel.yx;
        }else if (indexPath.row==5){
            if ([[us objectForKey:kRoleid] isEqualToString:@"2"]) {
                 cell.contentLab.text = @"教职工";
            }else{
                 cell.contentLab.text = @"学生";
            }
        }
        return cell;

    }else if (indexPath.section==2){
        MineInfoCell *cell = [[[NSBundle mainBundle] loadNibNamed:@"MineInfoCell" owner:nil options:nil] lastObject];
        cell.selectionStyle = UITableViewCellSelectionStyleDefault;
        cell.userInteractionEnabled=YES;
        cell.btn.hidden = NO;
        cell.titleLab.text = self.titleArray[indexPath.row+7];
        if ([[us objectForKey:kRoleid] isEqualToString:@"2"]) {
            if(indexPath.row==0) {
                cell.contentLab.text = self.minetInfoModel.sfzjh;
            }else if (indexPath.row==1) {
                cell.contentLab.text = self.minetInfoModel.phone;
            }
        }else{
            if(indexPath.row==0) {
                cell.contentLab.text = self.minetInfoModel.sfzjh;
            }else if (indexPath.row==1) {
                    cell.contentLab.text = self.minetInfoModel.yhkh;
                }else
                if (indexPath.row==2) {
                    cell.contentLab.text = self.minetInfoModel.phone;
                }
        }
        
        return cell;

    }
    return 0;
}


#pragma mark - 代理方法
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        return 90;
    } else if (indexPath.section == 1){
        return 45.5;
    } else if (indexPath.section == 2){
        return 44.5;
    }
    return 1;
}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return 10;
    } else if(section == 1) {
        return 15;
    } else if (section == 2){
        return 20;
    }

    return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.0001;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.section==0){
        if(indexPath.row==0){
            ChangeImageController * changeImageController = [[ChangeImageController alloc]init];
            changeImageController.url = _URL;
            [self.navigationController pushViewController:changeImageController animated:YES];
            
        }
    }
    else if(indexPath.section==2){
        if ([[us objectForKey:kRoleid] isEqualToString:@"2"]) {
             self.titleArray = @[@"头像",@"职工号",@"姓名",@"性别",@"出生年月",@"部门",@"职位",@"身份证",@"联系电话"];
        ReSetController *vc = [[ReSetController alloc] init];
        vc.reStr = self.titleArray[indexPath.row+7];
            if (indexPath.row==0) {
                vc.restr = _minetInfoModel.sfzjh;
            }else if (indexPath.row==1){
                vc.restr = _minetInfoModel.phone;

            }
        [self.navigationController pushViewController:vc animated:YES];
        }else{
            self.titleArray = @[@"头像",@"学号",@"姓名",@"性别",@"出生年月",@"院系",@"班级",@"身份证",@"银行卡号",@"联系电话"];
            ReSetController *vc = [[ReSetController alloc] init];
            vc.reStr = self.titleArray[indexPath.row+7];
            if (indexPath.row==0) {
                vc.restr = _minetInfoModel.sfzjh;
            }else if (indexPath.row==1){
                vc.restr = _minetInfoModel.yhkh;
                
            }else if (indexPath.row==2){
            vc.restr = _minetInfoModel.phone;
            }
            [self.navigationController pushViewController:vc animated:YES];
        }
    }
}

-(void)requestHeadImage
{
    NSDictionary *params = @{@"userid":[us objectForKey:kUserid]};
    NSString *BaseURL = [us objectForKey:baseUrl];
    [NetWorkManager requestWithType:1 withUrlString:@"/informationsite/getTitleImage.do" withParaments:params withSuccessBlock:^(id object) {
        if ([object[@"message"]isEqualToString:@"成功"]) {
            NSString * headImage = [NSString stringWithFormat:@"%@%@",BaseURL,object[@"imgUrl"]];
            _URL = [NSURL URLWithString:headImage];
            [self.tableView reloadData];
        }
        
    } withFailureBlock:^(NSError *error) {
        
        
    } progress:^(float progress) {
        
    }];
}


-(void)baoClick
{
    NSDictionary *params;
    if ([[us objectForKey:kRoleid] isEqualToString:@"2"]) {
       params  = @{@"userid":[us objectForKey:kUserid],
                                 @"sfzhm":self.minetInfoModel.sfzjh,
                                 @"phone":self.minetInfoModel.phone
                                 };
    }else{
        params  = @{@"userid":[us objectForKey:kUserid],
                    @"sfzhm":self.minetInfoModel.sfzjh,
                    @"phone":self.minetInfoModel.phone,
                    @"yhkh":self.minetInfoModel.yhkh
                    };
    
    }
   
    [NetWorkManager requestWithType:1 withUrlString:@"/selectInformation/updateInformation.do" withParaments:params withSuccessBlock:^(id object) {
        if ([object[@"status"] integerValue]==1) {
            [self.navigationController popViewControllerAnimated:YES];
        }
        
    } withFailureBlock:^(NSError *error) {
        
        
    } progress:^(float progress) {
        
    }];
}


-(void)SFZ:(NSNotification *)sender{
    //打印通知传过来的数值
    self.minetInfoModel.sfzjh = sender.userInfo[@"sfz"];
    [self.tableView reloadData];
}
-(void)YHKH:(NSNotification *)sender{
    //打印通知传过来的数值
    self.minetInfoModel.yhkh = sender.userInfo[@"yhkh"];
    [self.tableView reloadData];
}
-(void)LXDH:(NSNotification *)sender{
    //打印通知传过来的数值
    self.minetInfoModel.phone = sender.userInfo[@"lxdh"];
    [self.tableView reloadData];
}
-(void)head:(NSNotification *)sender{
    //打印通知传过来的数值
    _URL = [NSURL URLWithString:sender.userInfo[@"image"]];
    [self.tableView reloadData];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
